﻿Imports System.Text

''' <summary>
''' Socket 收发数据状态
''' </summary>
''' <remarks></remarks>
Public Class SocketInfo

    ''' <summary>
    ''' 数据发送方向
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum eDataType
        ''' <summary>
        ''' 此类的数据是从客户端接收到的数据
        ''' </summary>
        ''' <remarks></remarks>
        dRead

        ''' <summary>
        ''' 此类的数据时需要发送到客户端的数据
        ''' </summary>
        ''' <remarks></remarks>
        dWrite
    End Enum

    ''' <summary>
    ''' 客户端 Socket
    ''' </summary>
    ''' <remarks></remarks>
    Public [Socket] As Net.Sockets.Socket = Nothing

    ''' <summary>
    ''' 接收客户端发送数据的缓存最大值
    ''' </summary>
    ''' <remarks></remarks>
    Public Const BufferSize As Integer = 1024

    ''' <summary>
    ''' 数据收发缓存
    ''' </summary>
    ''' <remarks></remarks>
    Public buffer() As Byte

    ''' <summary>
    ''' 数据收发缓存 明文
    ''' </summary>
    ''' <remarks></remarks>
    Public Stringbuffer As New StringBuilder


    ''' <summary>
    ''' 往客户端发送的数据明文
    ''' </summary>
    ''' <remarks></remarks>
    Public DataType As eDataType = eDataType.dWrite


    ''' <summary>
    ''' 输出远程连接端信息
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Overrides Function toString() As String
        If [Socket] Is Nothing Then
            Return "NULL"
        Else
            Return [Socket].RemoteEndPoint.ToString
        End If
    End Function

    ''' <summary>
    ''' 已连接
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property Connected As Boolean
        Get
            Return mSocketState = SocketStateEnum.eConnected
        End Get
    End Property

    ''' <summary>
    ''' 连接状态
    ''' </summary>
    ''' <remarks></remarks>
    Private mSocketState As SocketStateEnum = SocketStateEnum.eCloseed
    ''' <summary>
    ''' 连接状态
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Property State As SocketStateEnum
        Get
            Return mSocketState
        End Get
        Set(ByVal value As SocketStateEnum)
            mSocketState = value
        End Set
    End Property

    ''' <summary>
    ''' socket连接状态
    ''' </summary>
    ''' <remarks></remarks>
    Public Enum SocketStateEnum
        ''' <summary>
        ''' 关闭中
        ''' </summary>
        ''' <remarks></remarks>
        eCloseed

        ''' <summary>
        ''' 正在查询远程主机DNS
        ''' </summary>
        ''' <remarks></remarks>
        eSearchDNS

        ''' <summary>
        ''' 正在连接远程主机
        ''' </summary>
        ''' <remarks></remarks>
        eConnecting


        ''' <summary>
        ''' 已连接远程主机
        ''' </summary>
        ''' <remarks></remarks>
        eConnected

        ''' <summary>
        ''' 正在监听端口
        ''' </summary>
        ''' <remarks></remarks>
        eListening

        ''' <summary>
        ''' 发生错误
        ''' </summary>
        ''' <remarks></remarks>
        eErr
    End Enum


    Public mKey As String

    ''' <summary>
    ''' 输出本地连接端信息
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function toLocalString() As String
        If [Socket] Is Nothing Then
            Return "NULL"
        Else
            Try
                Return [Socket].LocalEndPoint.ToString

            Catch ex As Exception

                Return "Err"
            End Try
        End If
    End Function
End Class